# coding=utf-8

from flask import Flask
from flask import request
from flask import render_template
import json
import data_util

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')

@app.route('/detail')
def detail():
    return render_template('detail.html')



def _to_dict(one_line):
    # 基金名称	代码	基金类型	基金规模	经理
    # 成立日	管理人	评级
    # 近1周	近1月	近3月	近6月	今年来	近1年	近2年	近3年
    one = {}
    one['fund_name'] = one_line[0]
    one['fund_code'] = one_line[1]
    one['fund_state'] = one_line[2]
    one['fund_scale'] = one_line[3]
    one['manager'] = one_line[4]
    one['fundation_date'] = one_line[5]
    one['fund_corp'] = one_line[6]
    one['grade'] = one_line[7]
    one['one_week'] = one_line[8]
    one['one_month'] = one_line[9]
    one['three_month'] = one_line[10]
    one['six_month'] = one_line[11]
    one['this_year'] = one_line[12]
    one['one_year'] = one_line[13]
    one['two_year'] = one_line[14]
    one['three_year'] = one_line[15]
    return one


@app.route('/all_info', methods=['POST', 'GET'])
def all_info():
    r = []
    for (k, v) in code_dic.items():
        r.append(_to_dict(v))
    return json.dumps(r)



@app.route('/search', methods=['POST', 'GET'])
def search():
    code = request.form.get('code')
    if not code:
        code = request.args.get('code')
    r = []
    for c in code_dic.keys():
        if str(c).startswith(code):
            r.append(code_dic[c])
    return json.dumps(r)


@app.route('/base_info', methods=['POST', 'GET'])
def base_info():
    type = request.form.get('type')
    if not type:
        type = request.args.get('type')
    value = request.form.get('value')
    if not value:
        value = request.args.get('value')
    print type, value
    if type == '0':
        result = json.dumps(_to_dict(name_dic[value]))
    else:
        result = json.dumps(_to_dict(code_dic[value]))
    return result


@app.route('/detail_by_code', methods=['POST', 'GET'])
def detail_by_code():
    code = request.form.get('code')
    if not code:
        code = request.args.get('code')
    return json.dumps(_to_dict(code_dic[code]))

@app.route('/compute_info', methods=['POST', 'GET'])
def compute_info():
    code = request.form.get('code')
    if not code:
        code = request.args.get('code')
    print code
    return json.dumps(compute_dic[code])


@app.route('/price_info', methods=['POST', 'GET'])
def price_info():
    code = request.form.get('code')
    if not code:
        code = request.args.get('code')
    print code
    return json.dumps(price_dic[code])


@app.route('/not_money_rate', methods=['POST', 'GET'])
def not_money_rate():
    code = request.form.get('code')
    if not code:
        code = request.args.get('code')
    print code
    return json.dumps(not_money_rate_dic[code])


if __name__ == '__main__':
    name_dic, code_dic = data_util.get_fund_base_info()
    compute_dic = data_util.get_compute()
    price_dic = data_util.get_history_price()
    not_money_rate_dic = data_util.not_money_rate()
    app.run(host='0.0.0.0', port=8080, debug=True)
